package org.lanqiao.util;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {
    public static Connection getConn(){
        String url = "jdbc:mysql://127.0.0.1:3306/jiawork?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";
        Connection connection = null;
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            connection =  DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    //释放资源的方法
    public static void closeAll(ResultSet rs, PreparedStatement pst, Connection conn) {
        try {
            if (rs !=null){
                rs.close();
            }
            if (pst !=null){
                pst.close();
            }
            if (conn !=null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //增删改的方法
    public static int executeUpdate(String sql,Object...objects){
        Connection conn = DBUtil.getConn();
        PreparedStatement pst = null;
        int row = -1;
        try {
            pst =conn.prepareStatement(sql);
            //给参数赋值
            for (int i=0;i<objects.length;i++){
                pst.setObject(i+1,objects[i]);
            }
            //4.执行sql
            row = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(null,pst,conn);
        }
        return row;
    }
    public static <T> List<T>  executeQuery(Class clazz,String sql,Object...objects){

        List<T> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            // 获取pst
            pst = conn.prepareStatement(sql);
            // 给参数赋值
            for (int i = 0; i < objects.length; i++) {
                pst.setObject(i + 1, objects[i]);
            }
            // 执行查询，处理返回结果
            rs = pst.executeQuery();
            // 获取元数据
            ResultSetMetaData rmd = rs.getMetaData();
            // 获取列数量
            int count = rmd.getColumnCount();
            while (rs.next()) {
                try {
                    T t = (T) clazz.newInstance();
                    for (int i = 0; i < count; i++) {
                        String label = rmd.getColumnLabel(i + 1);
                        Object obj = rs.getObject(label);
                        // 获取属性并给属性赋值
                        Field field = clazz.getDeclaredField(label);
                        field.setAccessible(true);
                        field.set(t, obj);
                    }
                    list.add(t);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pst, conn);
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(DBUtil.getConn());
    }
}
